﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;
using System.IO;
namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for KPIAchievementVerificatorHandler
    /// </summary>
    public class KPIAchievementVerificatorHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            int KPIAssignID = Convert.ToInt16(HttpContext.Current.Request.QueryString["KPIAssignID"]);
            int Year = Convert.ToInt16(HttpContext.Current.Request.QueryString["Year"]);
            int Status = Convert.ToInt16(HttpContext.Current.Request.QueryString["Status"]);
            string MethodDtlId = HttpContext.Current.Request.QueryString["MethodDtlId"];
            string Realization = HttpContext.Current.Request.QueryString["Realization"];
            string isLock = HttpContext.Current.Request.QueryString["isLock"];
            string Score = HttpContext.Current.Request.QueryString["Score"];
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));

            KPIAchievementFacade Af = new KPIAchievementFacade();
            List<KPIAchievement> list = new List<KPIAchievement>();
            KPIAchievement obj = new KPIAchievement();

            string[] ArrMethodDtlId = MethodDtlId.Split('|');
            string[] ArrRealization = Realization.Split('|');
            string[] ArrisLock = isLock.Split('|');
            string[] ArrScore = Score.Split('|');

            for (int i = 0; i < ArrMethodDtlId.Length - 1; i++)
            {
                if (ArrMethodDtlId[i].ToString().Trim() != "undefined")
                {
                    obj = new KPIAchievement();
                    obj.ID = 0;
                    obj.KPIAssign.ID = KPIAssignID;
                    obj.Year = Year;
                    obj.isActive = true;
                    //if (ArrisLock[i].ToString() == "1")
                    //    obj.isLocked = false;

                    obj.isLocked = ArrisLock[i].ToString() == "1" ? true : false;
                    obj.CreateBy = CurrUser.Email;
                    obj.Realization = ArrRealization[i].ToString();
                    obj.MethodDtlId = Convert.ToInt32(ArrMethodDtlId[i].ToString());
                    obj.Score = ArrScore[i].ToString();
                    obj.StatusByVerificator.ID = Status;
                    obj.LastModifiedBy = CurrUser.Email;

                    //HttpPostedFile file = HttpContext.Current.Request.Files[obj.MethodDtlId.ToString()];

                    ////check file was submitted
                    //if (file != null && file.ContentLength > 0)
                    //{
                    //    string fname = Path.GetFileName(file.FileName);
                    //    //file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
                    //}

                    list.Add(obj);
                }

            }


            using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                if (Af.SaveVerificator(list))
                    msg = "Transaction Success";
                else
                    msg = "Transaction Failed";

                trans.Complete();
                trans.Dispose();
            }


            return msg;
        }
    }
}